package bachelor.view {
	import bachelor.Globals;

	import gs.TweenLite;
	import gs.easing.Cubic;
	import gs.easing.Strong;

	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author Johannes
	 */
	public class Dock extends MovieClip 
	{
////////////////////////////////////////////////////////////////////////////////
/////// VARIABLES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
		
		// Allgemein 
		private var _dockState : String = "";
		private var _pulse : Timer;
		
		// MCs
		private var _screensaver 						: MovieClip = new MovieClip();
		private var _countryDockPlanePulsingInactive 	: MovieClip = MovieClip(new countryDockPlanePulsingInactiveMC());
		private var _countryDockPlanePulsingActive 		: MovieClip = MovieClip(new countryDockPlanePulsingActiveMC());
		private var _keyboard 							: Keyboard 	= new Keyboard();
		private var _keywordDockOutline 				: MovieClip = MovieClip(new keywordDockOutlineMC());
		private var _keywordDockOutlineSuche1 			: MovieClip = MovieClip(new keywordDockOutlineSuche1MC());
		private var _keywordDockOutlineSuche2 			: MovieClip = MovieClip(new keywordDockOutlineSuche2MC());
		private var _keywordDockCursor 					: MovieClip = new MovieClip();
		private var _keywordDockPlaneActive 			: MovieClip = MovieClip(new keywordDockPlaneActiveMC());
		private var _keywordDockPlanePulsingInactive 	: MovieClip = MovieClip(new keywordDockPlanePulsingInactiveMC());
		private var _keywordDockPlanePulsingActive 	 	: MovieClip = MovieClip(new keywordDockPlanePulsingActiveMC());
		private var _logo 								: MovieClip = MovieClip(new logoMC());
		
		// Größenwerte
		private var _screensaverAlpha : Number = 0.6;
		private var _pulsePhase : int = (Globals.fadeTime * 3) * 1000;
		
////////////////////////////////////////////////////////////////////////////////
/////// CONSTRUCTOR ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
		public function Dock() 
		{
//			trace("Dock -> Constructor ...");
			init();
		}




////////////////////////////////////////////////////////////////////////////////
/////// METHODS ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////



/////// INIT ///////////////////////////////////////////////////////////////////
		private function init() : void 
		{
//			trace("Dock -> init() ...");
			
			
			//--------init Screensaver -----------------------------------------
			_screensaver.graphics.beginFill(0x1e1e1e);
			_screensaver.graphics.drawRect(-512, -384, 1024, 768);
			_screensaver.graphics.endFill();
			_screensaver.x = 512;
			_screensaver.y = 384;
			_screensaver.alpha = _screensaverAlpha;
			_screensaver.visible = false;
			// Logo hier als MC einbauen
			addChild(_screensaver);
            
            
            //--------init logo ------------------------------------------------
			_logo.x = 512;
			_logo.y = 668;
			addChild(_logo);
			
			
            //--------init countryDockPlane ------------------------------------
			_countryDockPlanePulsingInactive.x = 512;
			_countryDockPlanePulsingInactive.y = 325;
			addChild(_countryDockPlanePulsingInactive);
			_countryDockPlanePulsingActive.x = 512;
			_countryDockPlanePulsingActive.y = 325;
			addChild(_countryDockPlanePulsingActive);

            
            //--------init keywordDock Schreiben -------------------------------
			_keywordDockOutlineSuche1.x = 512;
			_keywordDockOutlineSuche1.y = 100;
			_keywordDockOutlineSuche1.alpha = 0;
			_keywordDockOutlineSuche1.visible = false;
			addChild(_keywordDockOutlineSuche1);
			
			_keywordDockOutlineSuche2.x = 512;
			_keywordDockOutlineSuche2.y = 100;
			_keywordDockOutlineSuche2.alpha = 0;
			_keywordDockOutlineSuche2.visible = false;
			addChild(_keywordDockOutlineSuche2);
			
			_keywordDockCursor.graphics.beginFill(0xffffff);
			_keywordDockCursor.graphics.drawRect(-1.5, -32, 3, 64);
			_keywordDockCursor.graphics.endFill();
			_keywordDockCursor.x = 512;
			_keywordDockCursor.y = 100;
			_keywordDockCursor.alpha = 0;
			_keywordDockCursor.visible = false;
			addChild(_keywordDockCursor);
			
			_keyboard.x = 512;
			_keyboard.y = 275;
			_keyboard.alpha = 0;
			_keyboard.visible = false;
			addChild(_keyboard);


			//--------init keywordDock normal ----------------------------------
			_keywordDockPlanePulsingInactive.x = 512;
			_keywordDockPlanePulsingInactive.y = 100;
			addChild(_keywordDockPlanePulsingInactive);
			
			_keywordDockPlanePulsingActive.x = 512;
			_keywordDockPlanePulsingActive.y = 100;
			addChild(_keywordDockPlanePulsingActive);
			
			_keywordDockPlaneActive.x = 512;
			_keywordDockPlaneActive.y = 100;
			_keywordDockPlaneActive.alpha = 0;
			addChild(_keywordDockPlaneActive);
			
			_keywordDockOutline.x = 512;
			_keywordDockOutline.y = 100;
			_keywordDockOutline.alpha = 0.1;
			_keywordDockOutline.scaleX = 0.9;
			_keywordDockOutline.scaleY = 0.9;
			addChild(_keywordDockOutline);


            //--------init Puls-Takt -------------------------------------------
			_pulse = new Timer(_pulsePhase, 0);
			_pulse.addEventListener(TimerEvent.TIMER, onNextTimerPulseComplete);
			_pulse.start();


            //--------init DockState -------------------------------------------
			changeDockState("");
		}

		
		
/////// Dock-Status muss sich ändern ///////////////////////////////////////////
		public function changeDockState(p_dockState : String) : void 
		{
//			trace("Dock -> changeDockState() ... Status: " + p_dockState);
			
			_dockState = p_dockState;
			
			switch(_dockState) 
			{
			
				case "":
					hideKeyword();
					hideKeywordDockOutlineSuche1();
					moveKeywordDockPlaneRight();
					
					showScreensaver();
					deactivateKeywordOutline();
					showCountryPlanes();
					showLogo();
					break;
					
				case "country":
//					trace("=>=>=>=>=> Dock -> changeDockState() ::: country");
					hideKeyword();
					hideKeywordDockOutlineSuche1();
					moveKeywordDockPlaneRight();
					
					showScreensaver();
					deactivateKeywordOutline();
					hideCountryPlanes();
					showLogo();
					break;
					
				case "keyword":
//					trace("=>=>=>=>=> Dock -> changeDockState() ::: keyword");
					hideKeyword();
					hideKeywordDockOutlineSuche1();
					moveKeywordDockPlaneRight();
				
					activateKeywordOutline();
					showScreensaver();
					showCountryPlanes();
					showLogo();
					break;
					
				case "country+keyword":
//					trace("=>=>=>=>=> Dock -> changeDockState() ::: country+keyword");
					hideKeyword();
					hideKeywordDockOutlineSuche1();
					moveKeywordDockPlaneRight();
					
					hideScreensaver();
					activateKeywordOutline();
					hideCountryPlanes();
					hideLogo();
					break;

				case "writing":
//					trace("=>=>=>=>=> Dock -> changeDockState() ::: writing");
					showScreensaver();
					deactivateKeywordOutline();
					showCountryPlanes();
					showLogo();

					showKeyword();
					showKeywordDockOutlineSuche1();
					moveKeywordDockPlaneLeft();
					break;

				case "country+writing":
//					trace("=>=>=>=>=> Dock -> changeDockState() ::: country+writing");
					hideScreensaver();
					deactivateKeywordOutline();
					hideCountryPlanes();
					hideLogo();
					
					showKeyword();
					showKeywordDockOutlineSuche1();
					moveKeywordDockPlaneLeft();
					break;

			}
			
			
		}
		


/////// SCREENSAVER ////////////////////////////////////////////////////////////
		private function showScreensaver() : void 
		{
			_screensaver.visible = true;
			TweenLite.to(_screensaver,	Globals.fadeTime, {alpha: _screensaverAlpha, ease:Strong.easeIn});
		}
		private function hideScreensaver() : void 
		{
			TweenLite.to(_screensaver,	Globals.fadeTime, {alpha: 0,   ease:Strong.easeOut, onComplete: makeObjectUnvisible, onCompleteParams: [_screensaver]});
		}
		
		
		
/////// LOGO //////////////////////////////////////////////////////////////////
		private function showLogo() : void 
		{
			TweenLite.to(_logo,	Globals.fadeTime, {alpha: 1, y: 668, ease:Strong.easeIn});
		}
		private function hideLogo() : void 
		{
			TweenLite.to(_logo,	Globals.fadeTime, {alpha: 0, y: 868, ease:Strong.easeOut});
		}



/////// KEYWORD OUTLINE ////////////////////////////////////////////////////////
		private function activateKeywordOutline() : void 
		{
//			trace("Dock -> activateKeywordOutline() ...");
			TweenLite.to(_keywordDockPlaneActive, 	Globals.fadeTime, {alpha: 1, ease:Strong.easeIn});
			TweenLite.to(_keywordDockOutline,		Globals.fadeTime/4, {alpha: 1.0, scaleX: 1, scaleY: 1, ease:Strong.easeIn, overwrite: 2});
		}
		private function deactivateKeywordOutline() : void 
		{
//			trace("Dock -> deactivateKeywordOutline() ...");
			TweenLite.to(_keywordDockPlaneActive,	Globals.fadeTime, {alpha: 0,   ease:Strong.easeOut, overwrite: 1});
			TweenLite.to(_keywordDockOutline,		Globals.fadeTime, {alpha: 0.1, scaleX: 0.9, scaleY: 0.9, ease:Strong.easeOut, overwrite: 2});
		}


/////// COUNTRY PLANES /////////////////////////////////////////////////////////
		private function showCountryPlanes() : void 
		{
//			trace("Dock -> showCountryPlanes() ...");
			_countryDockPlanePulsingActive.visible 	 = true;
			_countryDockPlanePulsingInactive.visible = true;
			TweenLite.to(_countryDockPlanePulsingInactive, 	Globals.fadeTime, {alpha: 1, ease:Strong.easeIn});
			TweenLite.to(_countryDockPlanePulsingActive, 	Globals.fadeTime, {alpha: 1, ease:Strong.easeIn});
		}
		private function hideCountryPlanes() : void 
		{
//			trace("Dock -> hideCountryPlanes() ...");
			TweenLite.to(_countryDockPlanePulsingInactive, 	Globals.fadeTime, {alpha: 0, ease:Strong.easeOut, overwrite: 1, onComplete: makeObjectUnvisible, onCompleteParams: [_countryDockPlanePulsingInactive]});
			TweenLite.to(_countryDockPlanePulsingActive, 	Globals.fadeTime, {alpha: 0, ease:Strong.easeOut, overwrite: 1, onComplete: makeObjectUnvisible, onCompleteParams: [_countryDockPlanePulsingActive]  });
		}



/////// KEYBOARD ///////////////////////////////////////////////////////////////
		private function showKeyword() : void 
		{
//			trace("Dock -> showKeyword() ...");
			_keyboard.visible = true;
			TweenLite.to(_keyboard, Globals.fadeTime, {alpha: 1, ease:Strong.easeIn, overwrite: 1});
		}
		private function hideKeyword() : void 
		{
//			trace("Dock -> hideKeyword() ...");
			TweenLite.to(_keyboard, Globals.fadeTime, {alpha: 0, ease:Strong.easeOut, overwrite: 1, onComplete: makeObjectUnvisible, onCompleteParams: [_keyboard]});
		}


/////// KEYBOARD ///////////////////////////////////////////////////////////////
		private function showKeywordDockOutlineSuche1() : void 
		{
//			trace("Dock -> showKeywordDockOutlineSuche1() ...");
			_keywordDockOutlineSuche1.visible = true;
			TweenLite.to(_keywordDockOutlineSuche1, Globals.fadeTime, {alpha: 1, ease:Strong.easeIn, overwrite: 1});
			TweenLite.to(_keywordDockOutline,       Globals.fadeTime, {alpha: 0, ease:Strong.easeIn, overwrite: 1});
		}
		private function hideKeywordDockOutlineSuche1() : void 
		{
//			trace("Dock -> hideKeywordDockOutlineSuche1() ...");
			TweenLite.to(_keywordDockOutlineSuche1, Globals.fadeTime, {alpha: 0,   ease:Strong.easeOut, overwrite: 1, onComplete: makeObjectUnvisible, onCompleteParams: [_keywordDockOutlineSuche1]});
			TweenLite.to(_keywordDockOutline,       Globals.fadeTime, {alpha: 0.1, ease:Strong.easeIn, overwrite: 1});
		}
		
		
/////// POSITION KEYWORD PLANE /////////////////////////////////////////////////
		private function moveKeywordDockPlaneLeft() : void
		{
//			trace("Dock -> pulseKeywordDockPlane()");
			// Glow
			TweenLite.to(_keywordDockPlanePulsingActive, Globals.fadeTime*1.5,   {x: 158, ease:Cubic.easeOut, delay: Globals.fadeTime, overwrite: 1}); 
			// Fläche
			TweenLite.to(_keywordDockPlanePulsingInactive, Globals.fadeTime*1.5, {x: 158, ease:Cubic.easeOut, delay: Globals.fadeTime, overwrite: 1}); 
		}		
		private function moveKeywordDockPlaneRight() : void
		{
//			trace("Dock -> pulseKeywordDockPlane()");
			// Glow
			TweenLite.to(_keywordDockPlanePulsingActive, Globals.fadeTime*1.5,   {x: 512, ease:Cubic.easeOut, overwrite: 1}); 
			// Fläche
			TweenLite.to(_keywordDockPlanePulsingInactive, Globals.fadeTime*1.5,   {x: 512, ease:Cubic.easeOut, overwrite: 1}); 
		}		










////////////////////////////////////////////////////////////////////////////////
/////// OBJ PULSING ////////////////////////////////////////////////////////////


/////// Pulse-Takt Ende ////////////////////////////////////////////////////////
		private function onNextTimerPulseComplete(evt:TimerEvent) : void 
		{
//			trace("________________________PULSE");
			switch(_dockState) 
			{
			
				case "":
					pulseKeywordDockPlane();
					pulseCountryDockPlanes();
					break;
					
				case "country":
					pulseKeywordDockPlane();
					break;
					
				case "keyword":
					pulseCountryDockPlanes();
					break;

				case "writing":
					pulseCountryDockPlanes();
					pulseKeywordDockPlane();
					break;

				case "country+writing":
					pulseKeywordDockPlane();
					break;
					
			}
		}
		
		
		
		private function pulseKeywordDockPlane() : void
		{
//			trace("Dock -> pulseKeywordDockPlane()");
			// Glow
			TweenLite.to(_keywordDockPlanePulsingActive, Globals.fadeTime*1.5,   {alpha: 1, ease:Cubic.easeOut}); 
			TweenLite.to(_keywordDockPlanePulsingActive, Globals.fadeTime*1.5,   {alpha: 0.3, ease:Cubic.easeOut, delay: Globals.fadeTime*1.5}); 
			// Fläche
			TweenLite.to(_keywordDockPlanePulsingInactive, Globals.fadeTime*1.5,   {alpha: 1.0, ease:Cubic.easeOut}); 
			TweenLite.to(_keywordDockPlanePulsingInactive, Globals.fadeTime*1.5,   {alpha: 0.6, ease:Cubic.easeOut, delay: Globals.fadeTime*1.5}); 
		}
		
		
		private function pulseCountryDockPlanes() : void
		{
//			trace("Dock -> pulseCountryDockPlane()");
			// Glow
			TweenLite.to(_countryDockPlanePulsingActive, Globals.fadeTime*1.5,   {alpha: 1.0, ease:Cubic.easeOut}); 
			TweenLite.to(_countryDockPlanePulsingActive, Globals.fadeTime*1.5,   {alpha: 0.3, ease:Cubic.easeOut, delay: Globals.fadeTime*1.5}); 
			// Fläche
			TweenLite.to(_countryDockPlanePulsingInactive, Globals.fadeTime*1.5,   {alpha: 1.0, ease:Cubic.easeOut}); 
			TweenLite.to(_countryDockPlanePulsingInactive, Globals.fadeTime*1.5,   {alpha: 0.5, ease:Cubic.easeOut, delay: Globals.fadeTime*1.5}); 
		}


		
////////////////////////////////////////////////////////////////////////////////
/////// MAKE OBJ UNVISIBLE /////////////////////////////////////////////////////
		private function makeObjectUnvisible(p_obj : *) : void 
		{
//			trace("Dock -> makeObjectUnvisible() ... Object: " + p_obj);
			p_obj.alpha = 0;
			p_obj.visible = false;
		}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
	}
}
